<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class MUserMetas extends MModel
{
    
    public static function initUserMeta($userMeta) {
        if ($userMeta === null) {
            return null;
        }
        $user_meta_info = new MUserMetas();
        $user_meta_info->id             = $userMeta["id"];
        $user_meta_info->user_id        = $userMeta["user_id"];
        $user_meta_info->meta_key       = $userMeta["meta_key"];
        $user_meta_info->meta_value     = $userMeta["meta_value"];
        $user_meta_info->created_at     = $userMeta["created_at"];
        $user_meta_info->updated_at     = $userMeta["updated_at"];
        return $user_info;
    }
    
    
    public static function updateMetaByKey($user_id,$key,$value) {
        $sql  = "UPDATE " . DB_PREFIX . "_user_metas SET ";
        $sql .= "meta_value=\"$value\" ";
        $sql .= "WHERE user_id=$user_id AND meta_key=\"$key\"";
        FXP::trace("function: '{__FUNCTION__}',sql:'{$sql}'");
        $db_manager = MDbManager::getInstance();
        return $db_manager->updateDb($sql);
    }
    
    
    public function queryUserMetaByID($id) {
        $db = MDbManager::getInstance();
        $sql = "select * from ".DB_PREFIX."_user_metas where id=$id";
        return $db->selectDb($sql);
    }
    
    public static function queryMetaByKey($user_id, $key) {
        $sql  = "SELECT * FROM " . DB_PREFIX . "_user_metas WHERE ";
        $sql .= "user_id=$user_id AND meta_key=\"$key\"";
        FXP::trace("function: '{__FUNCTION__}',sql:'{$sql}'");
        $db_manager = MDbManager::getInstance();
        return $db_manager->selectDb($sql);
    }
    
    
    public function queryUserByPhone($phone) {
        $db = MDbManager::getInstance();
        $sql = "select * from ".DB_PREFIX."_user_metas where meta_key=\"phone\" and meta_value=\"{$phone}\"";

        FXP::trace("queryUserByPhone:".$sql);
        $db_data =  $db->selectDb($sql);
        
        if (empty($db_data)){
            return false;
        }
        return self::assembleUserMeta($db_data[0]);
    }
    
    
    public function queryUserByEmail($email) {
        $db = MDbManager::getInstance();
        $sql = "select * from ".DB_PREFIX."_user_metas where meta_key=\"email\" and meta_value=\"{$email}\"";

        FXP::trace("queryUserByEmail:".$sql);
        $db_data =  $db->selectDb($sql);
        
        if (empty($db_data)){
            return false;
        }
        return self::assembleUserMeta($db_data[0]);
    }
    
    
    public function queryMeta($key, $value) {
        $db = MDbManager::getInstance();
                $sql = "select * from ".DB_PREFIX."_user_metas where meta_key=\"{$key}\" and meta_value=\"{$value}\"";
        
        FXP::trace("queryMeta:".$sql);
        $db_data =  $db->selectDb($sql);
        
        if (empty($db_data)){
            return false;
        }
        return self::assembleUserMeta($db_data[0]);
    }
    
    
    
    public function queryMetaByIDAndKey($user_id, $key) {
        $db = MDbManager::getInstance();
                $sql = "select * from ".DB_PREFIX."_user_metas where user_id=$user_id and meta_key=\"{$key}\"";
        
        FXP::trace("queryMetaByIDAndKey:".$sql);
        $db_data =  $db->selectDb($sql);
        
        if (empty($db_data)){
            return false;
        }
        return self::assembleUserMeta($db_data[0]);
    }
    
    
    
    public function addUserMetas($user_id, $device_uuid, $token, $secret) {
        
    }

    
    
    private function assembleUserMeta($db_data){
        $this->id             = $db_data["id"];
        $this->user_id        = $db_data["user_id"];
        $this->meta_key       = $db_data["meta_key"];
        $this->meta_value     = $db_data["meta_value"];
        $this->created_at     = $db_data["created_at"];
        $this->updated_at     = $db_data["updated_at"];
        return $this;
    }
}
?>